package com.leesche.zcsp.node.web.controller.web.operation;

import com.leesche.zcsp.node.utils.web.PagingResult;
import com.leesche.zcsp.node.utils.web.messge.WebResult;
import com.leesche.zcsp.node.web.model.PubHardwaretype;
import com.leesche.zcsp.node.web.service.PubHardwaretypeService;
import com.leesche.zcsp.node.web.service.impl.data.read.PubHardwaretypeDto;
import com.leesche.zcsp.node.web.service.impl.data.read.QueryPubHardwaretypeCondition;
import com.leesche.zcsp.node.web.service.privilege.PrivilegeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.Map;

/**
 * Created by kaider on 2016/9/21.
 */
@Controller
@RequestMapping("/web/hardwaretype")
public class HardwaretypeController {
	private final static Logger LOGGER = LoggerFactory.getLogger(HardwaretypeController.class);

	private static final String LIST_VIEW = "web/hardwaretype/listView";
	private static final String ADD_VIEW = "web/hardwaretype/addView";
	private static final String EDIT_VIEW = "web/hardwaretype/editView";

	@Autowired
	private PubHardwaretypeService hardwareTypeService;

	@Autowired
	private PrivilegeService privilegeService;

	@RequestMapping(value = "/listView", method = RequestMethod.GET)
	public String listView() {
		return LIST_VIEW;
	}

	@ResponseBody
	@RequestMapping(value = "/list.json", method = RequestMethod.GET)
	public Map<String, Object> listJson(
			final QueryPubHardwaretypeCondition condition
	)
	{
		condition.setAdminId(privilegeService.getCurrentPrincipal().getUserid());
		PagingResult<PubHardwaretypeDto> pagingResult = hardwareTypeService.findAllByPage(condition);

		Map<String, Object> json = WebResult.buildOf(pagingResult).toMap();

		return json;
	}

	@RequestMapping(value = "/addView", method = RequestMethod.GET)
	public String addView() {
		return ADD_VIEW;
	}

	@ResponseBody
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public Map<String, Object> add(
			final PubHardwaretype hardwaretype
	)
	{
		try {

			hardwaretype.setAdminid(privilegeService.getCurrentPrincipal().getUserid());

			hardwareTypeService.insert(hardwaretype);

			return WebResult.success().info("操作成功！").toMap();

		} catch (Exception e) {
			LOGGER.error(e.getMessage(),e);
			return WebResult.failure(e).toMap();
		}
	}

	@RequestMapping(value = "/{id}/editView", method = RequestMethod.GET)
	public String editView(
			final @PathVariable("id") Long id,
			final Model model
	)
	{
		PubHardwaretype hardwaretype = hardwareTypeService.selectByPrimaryKey(id);

		model.addAttribute("hardwaretype", hardwaretype);

		return EDIT_VIEW;

	}

	@ResponseBody
	@RequestMapping(value = "/edit", method = RequestMethod.POST)
	public Map<String, Object> edit(final PubHardwaretype hardwaretype) {
		try {

			hardwaretype.setUpdatetime(new Date());

			hardwareTypeService.updateByPrimaryKeySelective(hardwaretype);

			return WebResult.success().info("操作成功！").toMap();

		} catch (Exception e) {
			LOGGER.error(e.getMessage(),e);
			return WebResult.failure(e).toMap();
		}
	}

	@ResponseBody
	@RequestMapping(value = "/{id}/delete", method = RequestMethod.POST)
	public Map<String, Object> delete(final @PathVariable("id") Long id) {
		try {

			hardwareTypeService.deleteByPrimaryKey(id);

			return WebResult.success().info("操作成功！").toMap();

		} catch (Exception e) {
			LOGGER.error(e.getMessage(),e);
			return WebResult.failure(e).toMap();
		}
	}
}
